﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MillerApp.DataAccess.MillerDB;
using MillerApp.DataContract.MillerDB;
using MillerApp.UserControls;
using MillerApp.UserControls.Enums;

namespace MillerApp.Stock
{
    public partial class CommonTypeForm : SubFormBase
    {
        //默认排序字段和顺次 [默认是按照证券代码升序排列]
        string column = "itemcode";
        SortDirection direction = SortDirection.DESC;

        public CommonTypeForm()
        {
            InitializeComponent();
        }

        private void CommonTypeForm_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.queryActions1.PagerControl = this.dataPagerControl1;
            this.queryActions1.OnQueryingData += new QueryingDataEventHandler(queryActions1_OnQueryingData);
            this.queryActions1.GetConditions += new GetStringDelegate(queryActions1_GetConditions);
            this.queryActions1.GetSortExpression += new GetSortExpression(queryActions1_GetSortExpression);
        }

        string queryActions1_GetConditions()
        {
            string conditions = string.Empty;
            if (string.IsNullOrEmpty(txtTypeName.Text)==false)
            {
                conditions += " and typename like '%" + txtTypeName.Text + "%'";
            }
            if (string.IsNullOrEmpty(this.txtItemName.Text) == false)
            {
                conditions += " and itemname like '%" + txtItemName.Text + "%'";
            }
            return conditions;
        }

        SortExpression queryActions1_GetSortExpression()
        {
            SortExpression exp = new SortExpression();
            exp.SortColumn = column;
            exp.SortDirection = direction;
            return exp;
        }

        void queryActions1_OnQueryingData(string conditions, string sortExpression, int pageIndex, int pageSize)
        {
            int recordCount = 0;
            List<CommontypeEntity> list = CommontypeManager.GetCommontypesByCond(conditions, sortExpression, pageIndex, pageSize, out recordCount);
            this.dataGridView1.DataSource = list;
            this.queryActions1.RecorderCount = recordCount;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            int col = e.ColumnIndex;

            object obj = dataGridView1.Rows[e.RowIndex].DataBoundItem;
            if (obj == null)
            {
                return;
            }
            CommontypeEntity dataItem = obj as CommontypeEntity;
            if (dataItem == null)
            {
                return;
            }

            if (col == 5)//编辑
            {
                CommonTypeMaintain editForm = new CommonTypeMaintain();
                editForm.Entity = dataItem;
                editForm.ActionTypeCode = ActionType.Modify;
                editForm.ShowDialog();
                if (editForm.DialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    queryActions1.OnQueryData();
                }
            }
            if (col == 6)//新增子类
            {
                CommonTypeMaintain editForm = new CommonTypeMaintain();
                editForm.Entity = new CommontypeEntity();
                editForm.Entity.parentcode = dataItem.itemcode;
                editForm.ActionTypeCode = ActionType.AddNew;
                editForm.ShowDialog();
                if (editForm.DialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    queryActions1.OnQueryData();
                }
            }
        }
    }
}
